package com.service.admin.support;


import com.service.match.common.entity.MatchOrder;
import com.service.match.common.utils.OrderCacheKeyUtil;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RList;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;


@Slf4j
@Component
public class RedissonUtils {


    @Autowired
    @Qualifier("matchEngineRedisson")
    private RedissonClient redissonClient;

    public RMap<BigDecimal, BigDecimal> getMatchMap(String key) {
        return redissonClient.getMap(key);
    }

    public RMap<BigDecimal, BigDecimal> getMatchMap(MatchOrder input) {
        return redissonClient.getMap(OrderCacheKeyUtil.getMatchKey(input.getSymbol(), input.isBuy()));
    }

    public RList<MatchOrder> getOrderBookList(String key) {
        return redissonClient.getList(key);
    }

    public void setOrderBook(MatchOrder input) {
        RList<MatchOrder> orderBook = getOrderBookList(OrderCacheKeyUtil.getOrderBookWithPriceKey(input.getSymbol(), input.isBuy(), input.getOrderPrice()));
        orderBook.add(input);
    }

    public RList<MatchOrder> getOppositeOrderBook(MatchOrder input, BigDecimal orderPrice){
        String oppositeKey = OrderCacheKeyUtil.getOrderBookWithPriceKey(input.getSymbol(), !input.isBuy(), orderPrice);
        return getOrderBookList(oppositeKey);
    }
}
